package com.wsd.smartcarwasher.controller;

import com.wsd.smartcarwasher.constans.Global;
import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.domain.*;
import com.wsd.smartcarwasher.service.MessagesService;
import com.wsd.smartcarwasher.service.PriceService;
import com.wsd.smartcarwasher.service.ProblemService;
import com.wsd.smartcarwasher.service.WxService;
import com.wsd.smartcarwasher.util.ReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Api(tags = {"客服相关接口"})
@Controller
@CrossOrigin
public class MessagesController {

    @Resource
    private WxService wxService;
    @Resource
    private MessagesService messagesService;
    @Resource
    private ProblemService problemService;


    @ApiOperation(
            value = "小程序－获取当前用户的聊天记录 ",
            notes = "小程序－获取当前用户的聊天记录",
            produces = "application/json",
            response = Messages.class
    )

    @RequestMapping(
            value = {"/v1/messages"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getMessages(HttpServletRequest request, @RequestParam int Page) {
        String sessionId = request.getHeader(Global.AUTHORIZATION);
        UserInfo user = wxService.getUserInfoBySessionId(sessionId);
        if (user == null) {
            return ReturnUtil.Error(ResultState.GET_USER_INFO_FAILED.getMsg());
        }
        return  ReturnUtil.Success("获取用户聊天信息成功",this.messagesService.getMessages(user.getUserId(),Page));
    }

    @ApiOperation(
            value = "获取用户留言列表",
            notes = "获取用户留言列表",
            produces = "application/json",
            response = Messages.class
    )

    @RequestMapping(
                value = {"/v1/messagesByM"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )

    @ApiImplicitParams({
            @ApiImplicitParam(  name = "mobile", value = "18702988981", required = false,  dataType = "String"),
    })
    @ResponseBody
    public ModelMap getMessages(@RequestParam int Page,@RequestParam int PageSize,HttpServletRequest request) {
        Messages m = new Messages();
        String mobile = request.getParameter("mobile");
        if(null!=mobile){
            m.setMobile(mobile);
        }
        return  ReturnUtil.Success("获取用户聊天信息成功",this.messagesService.getMessages(Page,PageSize,m));
    }



    @ApiOperation(
            value = "小程序－获取当前用户的聊天记录 ",
            notes = "小程序－获取当前用户的聊天记录",
            produces = "application/json",
            response = Messages.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(  name = "content", value = "{\"content\":\"12121\"}", required = true,  dataType = "{}"),
    })
    @RequestMapping(
            value = {"/v1/messages/addMsg"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap addMsg(HttpServletRequest request,@RequestBody Map<String, Object> content) {
        String sessionId = request.getHeader(Global.AUTHORIZATION);
        UserInfo user = wxService.getUserInfoBySessionId(sessionId);
        if (user == null) {
            return ReturnUtil.Error(ResultState.GET_USER_INFO_FAILED.getMsg());
        }
        Messages msg = new Messages();
        msg.setId(UUID.randomUUID().toString().replaceAll("-",""));
        msg.setContent(content.get("content").toString());
        msg.setContentType ("text");
        msg.setTime(new Date());
        msg.setSpeaker(user.getUserId());
        return  ReturnUtil.Success("留言成功",this.messagesService.addMsg(msg));
    }


    @ApiOperation(
            value = "后台－回复用户留言 ",
            notes = "后台－回复用户留言",
            produces = "application/json",
            response = Messages.class
    )

    @ApiImplicitParams({
            @ApiImplicitParam(  name = "parm", value = "{\"content\":\"12121\",\"消息Id\":\"2222\"}", required = true,  dataType = "{}"),
    })
    @RequestMapping(
            value = {"/v1/messages/addMsgByM"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap addMsgByM(HttpServletRequest request,@RequestBody Map<String, Object> parm){//@RequestParam String content,@RequestParam String userId) {
        Admin user = (Admin) SecurityUtils.getSubject().getPrincipal();
        if (user == null) {
            return ReturnUtil.Error(ResultState.GET_USER_INFO_FAILED.getMsg());
        }
        Messages msg = new Messages();
        msg.setId(parm.get("id").toString());
        msg.setBackContent(parm.get("content").toString());
        msg.setBackTime(new Date());
        msg.setAdminId(user.getId().toString());//user.getId().toString());
        return  ReturnUtil.Success("留言成功",this.messagesService.addMsByM(msg));
    }


   /* @ApiOperation(
            value = "小程序－获取常见问题解释说明 ",
            notes = "小程序－获取常见问题解释说明",
            produces = "application/json",
            response = Problem.class
    )

    @RequestMapping(
            value = {"/v1/probelm"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getProblem() {
        return  ReturnUtil.Success("获取常见问题说明成功",this.problemService.getProblem());
    }*/


    @ApiOperation(
            value = "pc端-删除留言 ",
            notes = "pc端-删除留言",
            produces = "application/json",
            response = Problem.class
    )

    @RequestMapping(
            value = {"/v1/messages/delete"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap delete(@RequestParam String id) {

        int rows = this.messagesService.delete(id);
        if(rows>0){

            return  ReturnUtil.Success("删除成功");
        }
        return  ReturnUtil.Error("删除失败");
    }



}
